package com.xyzq.webapp.action.sm;

import java.util.Date;
import java.util.List;

import org.appfuse.model.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.opensymphony.xwork2.Preparable;
import com.xyzq.model.common.DataDictionary;
import com.xyzq.model.sm.OrderSell;
import com.xyzq.service.common.DataDictionaryManager;
import com.xyzq.service.sm.OrderSellManager;
import com.xyzq.webapp.action.BaseAction;

public class OrderSellAction extends BaseAction implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9034799500891444596L;
	private OrderSellManager orderSellManager;
	private DataDictionaryManager dataDictionaryManager;
	private List<DataDictionary> productNameGroup;
	private List<DataDictionary> marketGroup;
	private OrderSell orderSell;
	private List<OrderSell> orderSellGroup;
	private Long id;
	private boolean subSystem;

	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		this.productNameGroup = getDataDictionaryManager().findByModule(
				"orderSellProductName");
		this.marketGroup = getDataDictionaryManager().findByModule(
				"orderSellMarket");
	}

	public String list() {
		orderSellGroup = this.orderSellManager.getAll();
		this.logUserAction("orderSellAction", "read all list");
		return SUCCESS;
	}

	public String userList() {
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		User user = (User) auth.getPrincipal();
		orderSellGroup = this.orderSellManager.findByUser(user);
		this.logUserAction("orderSellAction", "read self list");
		return SUCCESS;
	}

	public String delete() {
		this.getOrderSellManager().remove(id);
		saveMessage(getText("orderSell.deleted"));
		this.logUserAction("orderSellAction", "delete:" + this.getOrderSell());
		return SUCCESS;
	}

	public String edit() {
		if (this.getId() != null) {
			this.orderSell = this.orderSellManager.get(getId());
			Authentication auth = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auth.getPrincipal();
			if (!user.getId().equals(orderSell.getUser().getId())) {
				this.orderSell = new OrderSell();
			}
			this.logUserAction("orderSellAction",
					"ready edit:" + this.getOrderSell());
		} else {
			this.orderSell = new OrderSell();
			this.logUserAction("orderSellAction",
					"ready input:" + this.getOrderSell());
		}
		this.subSystem = false;
		return SUCCESS;
	}

	public String editSubSystem() {
		if (this.getId() != null) {
			this.orderSell = this.orderSellManager.get(id);
			this.subSystem = true;
			this.logUserAction("orderSellAction",
					"ready edit:" + this.getOrderSell());
		} else {
			this.orderSell = new OrderSell();
			this.subSystem = false;
			this.logUserAction("orderSellAction",
					"ready input:" + this.getOrderSell());
		}
		return SUCCESS;
	}

	public String save() {
		if (cancel != null) {
			return CANCEL;
		}
		if (delete != null) {
			return delete();
		}
		boolean isNew = (this.orderSell.getId() == null);
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		User user = (User) auth.getPrincipal();
		if (isNew) {
			orderSell.setUser(user);
			orderSell.setSubTime(new Date());
		} else {
			OrderSell os = this.getOrderSellManager().get(
					this.orderSell.getId());
			orderSell.setUser(os.getUser());
			orderSell.setSubTime(os.getSubTime());
			orderSell.setModifyTime(new Date());
		}
		this.getOrderSellManager().save(orderSell);
		String key = (isNew) ? "orderSell.added" : "orderSell.updated";
		saveMessage(getText(key));
		if (!isNew) {
			this.logUserAction("orderSellAction", "edit:" + this.getOrderSell());
			return INPUT;
		} else {
			this.logUserAction("orderSellAction", "save:" + this.getOrderSell());
			return SUCCESS;
		}
	}

	public OrderSellManager getOrderSellManager() {
		return orderSellManager;
	}

	public DataDictionaryManager getDataDictionaryManager() {
		return dataDictionaryManager;
	}

	public List<DataDictionary> getProductNameGroup() {
		return productNameGroup;
	}

	public List<DataDictionary> getMarketGroup() {
		return marketGroup;
	}

	public OrderSell getOrderSell() {
		return orderSell;
	}

	public Long getId() {
		return id;
	}

	public void setOrderSellManager(OrderSellManager orderSellManager) {
		this.orderSellManager = orderSellManager;
	}

	public void setDataDictionaryManager(
			DataDictionaryManager dataDictionaryManager) {
		this.dataDictionaryManager = dataDictionaryManager;
	}

	public void setProductNameGroup(List<DataDictionary> productNameGroup) {
		this.productNameGroup = productNameGroup;
	}

	public void setMarketGroup(List<DataDictionary> marketGroup) {
		this.marketGroup = marketGroup;
	}

	public void setOrderSell(OrderSell orderSell) {
		this.orderSell = orderSell;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setOrderSellGroup(List<OrderSell> orderSellGroup) {
		this.orderSellGroup = orderSellGroup;
	}

	public List<OrderSell> getOrderSellGroup() {
		return orderSellGroup;
	}

	public boolean isSubSystem() {
		return subSystem;
	}

	public void setSubSystem(boolean subSystem) {
		this.subSystem = subSystem;
	}

}
